package com.scm.controller;

/**
 * @author CHL
 * @ClassName: LoginController
 * @Description: TODO
 * @date 2022/3/8 14:31
 * @Version 1.0
 */
import com.scm.dao.ScmuserMapper;
import com.scm.dao.UserModelMapper;
import com.scm.entity.Scmuser;
import com.scm.entity.UserModel;
import com.scm.entity.response.ResponseMessage;
import com.scm.service.IUserService;
import com.scm.util.TokenUtil;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/sys"})
public class LoginController
{
    @Autowired
    private ScmuserMapper scmuserMapper;
    @Autowired
    private UserModelMapper userModelMapper;
    @Autowired
    private IUserService userService;

    @RequestMapping({"/login"})
    public ResponseMessage login(String username, String password, String role)
            throws UnsupportedEncodingException
    {
        ResponseMessage rm = new ResponseMessage();
        System.out.println("登录用户名" + username + "," + password);
        if ((username == null) || (password == null) || (username.matches("\\s*")) || (password.matches("\\s*")))
        {
            rm.setCode(Integer.valueOf(3));
            rm.setMessage("帐号密码不能为空");
            return rm;
        }
        if ("customer".equals(role))
        {
            rm.setCode(Integer.valueOf(1));
            rm.setMessage("暂不支持客户登录");
            return rm;
        }
        Map<String, Object> map = new HashMap();
        map.put("username", username);
        map.put("password", password);
        Scmuser user = this.scmuserMapper.login(map);
        if (user != null)
        {
            if (user.getStatus().intValue() == 0)
            {
                List<UserModel> models = this.userModelMapper.selectUserModel(username);
                user.setModels(models);
                System.out.println(user + "登录成功");
                rm.setCode(Integer.valueOf(2));
                rm.setMessage("登录成功");
                HashMap<String, Object> hm = new HashMap();
                hm.put("token", TokenUtil.createToken(user.getAccount(), user.getName()));
                hm.put("user", user);
                rm.setData(hm);
            }
            else
            {
                rm.setCode(Integer.valueOf(4));
                rm.setMessage("账户锁定联系管理员");
            }
        }
        else
        {
            rm.setCode(Integer.valueOf(3));
            rm.setMessage("账户密码错误");
        }
        return rm;
    }

    @RequestMapping({"/logout"})
    public ResponseMessage logout()
    {
        ResponseMessage rm = new ResponseMessage();
        rm.setCode(Integer.valueOf(2));
        rm.setMessage("退出成功");

        return rm;
    }
}

